<template>
  <div class="atlas-list">
    <div class="card-list">
      <div class="card-box" v-for="item in articleList" :key="item.id">
        <div class="card">
          <a
            href="javascript:;"
            class="card-img"
            @click="handleDetails(item.path, '/content-article/' + item.id)"
            ><img :src="item.imgHost + item.imgUrl" alt=""
          /></a>
          <a
            href="javascript:;"
            class="card-tag"
            @click="handleDetails(item.path)"
            >{{ item.tag }}</a
          >
          <a
            href="javascript:;"
            class="card-title"
            @click="handleDetails(item.path, '/content-article/' + item.id)"
            >{{ item.title }}</a
          >
          <div class="card-foot">
            <div>{{ item.createTime }}</div>
            <div>
              <span class="iconfont icon-dianzan1">{{ item.like_num }}</span>
              <span class="iconfont icon-Icon-guankanliang">{{
                item.look_num
              }}</span>
              <span class="iconfont icon-xiaoxi">0</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="isMinScreen">
      <div class="pagination" v-if="totalPage > 1">
        <div class="pre" v-show="currentPage !== 1" @click="handlePre">
          上一页<i class="iconfont icon-you"></i>
        </div>
        <div
          class="next"
          v-show="currentPage !== totalPage"
          @click="handledNext"
        >
          下一页<i class="iconfont icon-zuo"></i>
        </div>
      </div>
    </div>
    <div v-else>
      <div class="pagination" v-if="totalPage > 1">
        <span class="total-page">{{ currentPage + " / " + totalPage }}</span>
        <div class="pre" v-show="currentPage !== 1" @click="handlePre">
          <i class="iconfont icon-you"></i>
        </div>
        <div
          class="page-num"
          v-for="item in numList"
          :key="item"
          @click="handlePage(item)"
          :style="
            currentPage == item ? { background: '#0088cc', color: '#fff' } : {}
          "
        >
          {{ item }}
        </div>
        <div
          class="next"
          v-show="currentPage !== totalPage"
          @click="handledNext"
        >
          <i class="iconfont icon-zuo"></i>
        </div>
        <div class="input-page">
          <input
            type="text"
            placeholder="页码"
            v-model="goPage"
            @keydown.enter="handleGoPage"
          />
          <i @click="handleGoPage" class="iconfont icon-youjiantou"></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { getArticle } from "@/api/api";
import { mapMutations, mapActions } from "vuex";
export default {
  name: "AtlasList",
  data() {
    return {
      path: "",
      isMinScreen: false, //是否为小屏幕
      currentPage: 1, //当前页码
      totalPage: 1, //总页数
      numList: [], //页码列表
      articleList: [], //数据列表
      goPage: "",
    };
  },
  async fetch() {
    this.currentPage = parseInt(this.$route.params.page) || this.currentPage;

    this.numList = [];
    this.path =
      this.$route.path.indexOf("/page/") != -1
        ? this.$route.path.slice(0, this.$route.path.indexOf("/page/"))
        : this.$route.path;
    const res = await this.$api.getArticle({
      path: this.path,
      num: 40,
      currentPage: this.currentPage,
    });
    this.articleList = res.data;
    this.totalPage = res.totalPage;
    if (this.totalPage > 6 && this.currentPage > 4) {
      if (this.currentPage == this.totalPage) {
        for (let i = this.currentPage - 6; i < this.currentPage; i++) {
          this.numList.push(i + 1);
        }
      } else if (this.currentPage + 1 == this.totalPage) {
        for (let i = this.currentPage - 5; i < this.currentPage + 1; i++) {
          this.numList.push(i + 1);
        }
      } else {
        for (let i = this.currentPage - 4; i < this.currentPage + 2; i++) {
          this.numList.push(i + 1);
        }
      }
    } else {
      for (let i = 0; i < this.totalPage; i++) {
        if (i > 5) break;
        this.numList.push(i + 1);
      }
    }
    this.$cookies.set("activePage", this.currentPage);
    this.changePage(this);
  },
  mounted() {
    this.setResize();
    window.addEventListener("resize", this.setResize);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.setResize);
  },
  methods: {
    ...mapMutations(["changePage"]),
    ...mapActions(["handleSetActiveArr"]),
    setResize() {
      if (window.innerWidth < 768) {
        this.isMinScreen = true;
      } else {
        this.isMinScreen = false;
      }
    },
    handlePre() {
      this.currentPage--;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
    },
    handledNext() {
      this.currentPage++;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
    },
    handlePage(page) {
      this.currentPage = page;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
    },
    handleGoPage() {
      // console.log(typeof this.goPage);
      let page = parseInt(this.goPage);
      if (typeof page != "number") {
        return;
      }
      if (page < 1 || page > this.totalPage) {
        alert("超出范围");
        return;
      }
      this.currentPage = page;
      this.$router.push(`${this.path}/page/${this.currentPage}`);
      this.goPage = "";
    },
    async handleDetails(path, pathId) {
      await this.handleSetActiveArr({ path, app: this });
      if (pathId) {
        this.$router.push({
          path: pathId,
        });
      } else {
        this.$router.push({
          path,
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
@media only screen and (min-width: 1240px) {
  .card-box {
    width: 25%;
    .card {
      .card-img {
        height: 175px;
      }
    }
  }
}
@media only screen and (min-width: 992px) and (max-width: 1240px) {
  .card-box {
    width: 25%;
    .card {
      .card-img {
        height: 139px;
      }
    }
    .card-foot {
      div {
        span {
          &:nth-last-child(1) {
            display: none;
          }
        }
      }
    }
  }
}
@media only screen and (max-width: 992px) {
  .card-list {
    .card-box {
      width: 50%;
      .card {
        .card-img {
          height: 222px;
        }
      }
    }
  }
}

@media only screen and (max-width: 768px) {
  .card-list {
    .card-box {
      .card {
        .card-img {
          height: initial;
        }
      }
      .card-foot {
        div {
          white-space: initial;
          span {
            &:nth-last-child(1),
            &:nth-last-child(2) {
              display: none;
            }
          }
        }
      }
    }
  }
}
.atlas-list {
  box-sizing: border-box;
  padding-bottom: 10px;
  background: #fff;
  .card-list {
    box-sizing: border-box;
    transition: transform 1s;
    margin-bottom: 10px;
    .card-box {
      display: inline-block;
      padding: 10px;
      box-sizing: border-box;
      .card {
        box-sizing: border-box;
        width: 100%;
        flex: 1;
        border: 1px solid #eeeeee;
        overflow: hidden;
        border-radius: 5px;
        position: relative;
        transition: all 0.3s;
        top: 0;
        background: #fff;
        .card-img {
          display: flex;
          justify-content: center;
          align-items: center;
          overflow: hidden;
          img {
            width: 100%;
          }
        }
        .card-tag {
          width: initial;
          height: initial;
          position: absolute;
          top: 15px;
          left: 15px;
          padding: 5px 10px;
          margin: 0;
          font-size: 12px;
          border-radius: 5px;
          color: #fff;
          background: rgba(0, 0, 0, 0.6);
          &:hover {
            color: #fff;
            background: #3361ea;
          }
        }
        .card-title {
          width: calc(100% - 28px);
          height: 42px;
          white-space: normal;
          margin: 14px;
          box-sizing: border-box;
          color: #000;
          &:hover {
            color: #3361ea;
          }
          line-height: 1.4;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .card-foot {
          display: flex;
          justify-content: space-between;
          margin: 0 15px 15px;
          div {
            color: #aaa;
            font-size: 12px;
            .iconfont {
              font-size: 12px;
              margin-left: 5px;
              &::before {
                margin-right: 2px;
              }
            }
            .icon-xiaoxi {
              cursor: pointer;
            }
          }
        }
        &:hover {
          top: -5px;
          box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
        }
      }
    }
  }
  .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    color: rgba(56, 56, 56, 0.8);
    text-align: center;
    font-size: 13px;
    span {
      height: 28px;
      line-height: 28px;
      border-radius: 2px;
      padding: 0 8px;
      margin: 0 5px;
      background: #f4f4f5;
    }
    div {
      // width: 30px;
      // height: 28px;
      line-height: 28px;
      box-sizing: border-box;
      border-radius: 2px;
      padding: 0 8px;
      margin: 0 5px;
      background: #f4f4f5;
      cursor: pointer;
      &:hover {
        color: #fff;
        background: #3361ea;
      }
    }
    .input-page {
      position: relative;
      width: 66px;
      height: 28px;
      padding: 0;
      display: flex;
      align-items: center;
      input {
        width: 66px;
        height: 28px;
        padding-left: 10px;
        border: 1px solid #eee;
        box-sizing: border-box;
        outline: none;
      }
      i {
        width: 28px;
        position: absolute;
        top: 0;
        right: 0;
        padding: 0 5px;
        box-sizing: border-box;
        color: #333;
        &:hover {
          color: #0088cc;
        }
      }
    }
  }
}
</style>